/*
 * @Author: your name
 * @Date: 2022-03-23 21:08:55
 * @LastEditTime: 2022-03-26 00:52:19
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \RVMDK（uv5）c:\Users\wangzhen\Desktop\张毕设\程序代码\毕设程序\Zigbee_Server\Components\TIME\src\time.c
 */
#include "time.h"

/*时间结构体，默认时间2000-01-01 00:00:00*/
struct rtc_time systmtime =
{
    0, 0, 0, 1, 1, 2000, 0
};

/**
 * @brief 时间结构体初始化
 */
void time_init(void)
{
    RTC_NVIC_Config();
    Time_Adjust(&systmtime);
}

/**
 * @brief 设置当前时间;
 * 
 * @param timeStamp : 时间戳
 */
void time_set(uint32_t timeStamp)
{
    struct rtc_time tm={0};
	timeStamp+=TIME_ZOOM;
    to_tm(timeStamp, &tm);
    Time_Adjust(&tm);
}

/**
 * @brief 获取当前时间
 * 
 * @return uint32_t 
 * @retval 
 *   时间戳
 */
uint32_t time_get(void)
{
	return RTC_GetCounter();
}

#include "stdio.h"

/**
 * @brief 时间戳 转 字符串 [北京时间]
 * 
 * @param timeStamp 时间戳 
 * @return char* 
 */
char* time_to_str(uint32_t timeStamp)
{
	struct rtc_time tm={0};
	static char dataBuf[128]={0};
    int Len = 0;
	
	// printf("timeStamp: %d\n", timeStamp);
	/*  把标准时间转换为北京时间*/
    timeStamp = timeStamp + TIME_ZOOM;
	
	/*把定时器的值转换为北京时间*/
    to_tm(timeStamp, &tm);
		
	/* 输出时间戳，公历时间 */
    Len = sprintf(dataBuf, "%d/%d/%d %0.2d:%0.2d:%0.2d",\
            tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
    dataBuf[Len]=0;

	return dataBuf;
}

